<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- card 卡片 -->
      <el-card class="box-card">
        <!-- tabs -->
        <el-tabs v-model="activeName">
          <el-tab-pane class="tab-pane" label="组织架构" name="first">
            <!-- layout布局 -->
            <el-row class="department-header">
              <!-- 左侧 -->
              <el-col :span="20">
                <i class="el-icon-s-home" />
                <span class="company">江苏传智播客教育科技股份有限公司</span>
              </el-col>
              <!-- 右侧 -->
              <el-col :span="4">
                <el-row type="flex">
                  <el-col> 负责人 </el-col>
                  <el-col>
                    <el-dropdown>
                      <span class="el-dropdown-link">
                        操作<i class="el-icon-arrow-down el-icon--right" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDeptsFn('')">添加部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

            <!-- 树形控件 -->
            <el-tree class="departments-tree" :data="treeData" :props="defaultProps" default-expand-all>
              <!-- 插槽 -->
              <!--插槽本身提供了两个数据: data:就是当前行的数据-->
              <template v-slot="{data}">
                <el-row type="flex" justify="space-between" style="width:100%">
                  <!-- 左侧 -->
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <!-- 右侧 -->
                  <el-col :span="4">
                    <el-row type="flex">
                      <el-col> {{ data.manager }} </el-col>
                      <el-col>
                        <el-dropdown>
                          <span class="el-dropdown-link">
                            操作<i class="el-icon-arrow-down el-icon--right" />
                          </span>
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item @click.native="addDeptsFn(data.id)">添加部门</el-dropdown-item>
                            <el-dropdown-item @click.native="editDeptsFn(data.id)">编辑部门</el-dropdown-item>
                            <!-- 如果当前部门有子部门,当前部门不能够显示删除部门文字 -->
                            <el-dropdown-item v-if="data && !data.children" @click.native="delDeptsFn(data.id)">删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>

              </template>
            </el-tree>
          </el-tab-pane>

        </el-tabs>

        <!-- 部门弹框组件 -->
        <el-dialog
          :title="isEdit ? '编辑部门' :'添加部门'"
          :visible.sync="dialogShow"
          width="50%"
          :close-on-press-escape="false"
          :close-on-click-modal="false"
          :center="true"
        >
          <dept-dialog v-if="dialogShow" :parent-id="parentId" :is-edit="isEdit" :origin-list="originList" @updateList="updateListFn" @close="closeConfirmFn" />
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import DeptDialog from './DeptDialog'
export default {
  components: {
    DeptDialog
  },
  data() {
    return {
      activeName: 'first',
      // 树形控件数据
      treeData: [],
      // 定义结构显示
      defaultProps: {
        children: 'children', // 指定描述父子关系的字段
        label: 'name' // 渲染数据中的某一个字段名，让用户进行查看，渲染哪一个字段的文字
      },
      dialogShow: false, // 弹框的显示
      parentId: '', // 父级id
      isEdit: false, //  控制是否是编辑
      originList: [] // 简化后的部门数据
    }
  },
  async created() {
    // 获取部门列表
    this.getDepartments()
  },
  methods: {
    // 编辑部门
    editDeptsFn(id) {
      this.dialogShow = true
      this.parentId = id
      this.isEdit = true
    },

    // 获取部门列表
    async getDepartments() {
      const res = await getDepartments()
      // 简化部门数据
      this.originList = res.data.depts.map(item => ({
        name: item.name,
        id: item.id,
        code: item.code,
        pid: item.pid
      }))
      // console.log(this.originList)
      this.treeData = tranListToTreeData(res.data.depts)
    },

    addDeptsFn(id) {
      console.log(id)
      this.parentId = id
      this.dialogShow = true
      this.isEdit = false
    },

    // 关闭弹窗
    // 重新刷新列表
    updateListFn() {
      this.dialogShow = false
      this.getDepartments()
    },

    // 关闭弹框
    closeConfirmFn() {
      this.dialogShow = false
    },
    // 删除部门
    async delDeptsFn(id) {
      // 提示弹框
      const confirmRes = await this.$confirm('此操作将删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 点击取消
      if (confirmRes === 'cancel') return this.$message('取消删除')
      // 点击确定 --> 发请求 --> 提示 --> 刷新页面
      const res = await delDepartment(id)
      // 提示框
      this.$message.success(res.message)
      // 重新渲染页面
      this.getDepartments()
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  padding: 10px 30px;
}
.departments-tree {
  margin: 15px 0;
}
.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
